<?php
namespace EApiSwoole\Support\Facades;

abstract class Facade
{
    protected static $app;
    protected static $resolvedInstances = [];
    protected abstract static function getFacadeAccessor();
    public static function getFacadeRoot()
    {
        return static::resolveFacadeInstance(static::getFacadeAccessor());
    }

    protected static function resolveFacadeInstance($object)
    {
        if (\is_object($object)) {
            return $object;
        }
        if (isset(static::$resolvedInstances[$object] )) {
            return static::$resolvedInstances[$object];
        }
        return static::$resolvedInstances[$object] = static::$app[$object];
    }
    public static function setFacadeApplication($app)
    {
        static::$app = $app;
    }
    public static function __callStatic($method, $params = [])
    {
        $instance = static::getFacadeRoot();
        if (! $instance) {
            throw new \Exception('对不起，没有找到可解析的实例对象。', 500);
        }
        return $instance->$method(...$params);

    }
}